//
//  BN_KinderGartenHelper.m
//  BabyNow
//
//  Created by 闫建刚 on 14-5-20.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_KinderGartenHelper.h"
#import "IMB_NetWorkJsonRespOperation.h"
#import "IMB_NetWorkAccessOperationWithFilesUpload.h"
#import "BN_UserInfo.h"
#import "BN_UserHelper.h"


@interface BN_KinderGartenHelper()
{
    int foodListPageNo_;    // 美食列表页码
    int actionListPageNo_;  // 活动列表页码
    int suggestListPageNo_; // 园长信箱列表亚麻
    
    NSMutableArray *foodListArr_;       // 美食列表集合
    NSMutableArray *actionListArr_;     // 活动列表集合
    NSMutableArray *suggestListArr_;    // 园长信箱列表集合
    
    // 文件上传任务
    IMB_NetWorkAccessOperationWithFilesUpload *uploadOperation_;
    
    // 上传进度回调处理
    UploadProcessingHandler uploadProcessingHandler_;
}

/**
 *  查询美食列表队列
 */
@property (nonatomic,strong) NSOperationQueue *queryFoodListQueue;

/**
 *  查询活动列表队列
 */
@property (nonatomic,strong) NSOperationQueue *queryActionListQueue;

/**
 *  查询美食详情队列
 */
@property (nonatomic,strong) NSOperationQueue *queryFoodDetailQueue;

/**
 *  查询活动详情队列
 */
@property (nonatomic,strong) NSOperationQueue *queryActionDetailQueue;

/**
 *  查询园长信箱列表队列
 */
@property (nonatomic,strong) NSOperationQueue *querySuggestListQueue;

/**
 *  查询园长信箱详情队列
 */
@property (nonatomic,strong) NSOperationQueue *querySuggestInfoQueue;

/**
 *  发布信息队列
 */
@property (nonatomic,strong) NSOperationQueue *sendInfoQueue;

/**
 *  查询指定页码的美食列表
 *
 *  @param pageNo 页码
 */
- (void)queryFoodListWithPageNo:(int)pageNo;

/**
 *  查询指定页码的活动列表
 *
 *  @param pageNo 页码
 */
- (void)queryActionListWithPageNo:(int)pageNo;

/**
 *  查询指定页码的园长信箱列表
 *
 *  @param pageNo 页码
 */
- (void)querySuggestListWithPageNo:(int)pageNo;

@end

@implementation BN_KinderGartenHelper

#pragma mark - Extend method

// 查询指定页码的园长信箱列表
- (void)querySuggestListWithPageNo:(int)pageNo{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_SUGGEST_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kPageno:[NSString stringWithFormat:@"%d",pageNo]} completeHandler:^(id result, NSString *errorMessage) {
        
        if (result) {
            if([result[kErrorCode]intValue] == 0){
                NSArray *info = result[kinfo];
                if(info.count > 0){
                    suggestListPageNo_= pageNo;
                    if(pageNo == 1){
                        [suggestListArr_ removeAllObjects];
                    }
                    [suggestListArr_ addObjectsFromArray:info];
                    self.querySuggestListCompleteHandler(suggestListArr_,errorMessage);
                }else{
                    if (pageNo == 1) {
                        self.querySuggestListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.querySuggestListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.querySuggestListCompleteHandler(nil,result[kErrorMessage]);
            }
        }else{
            self.querySuggestListCompleteHandler(nil,errorMessage);
        }
    }];
    [self.querySuggestListQueue addOperation:operation];
}

// 查询指定页码的美食列表
- (void)queryFoodListWithPageNo:(int)pageNo{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_FOOD_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kPageno:[NSString stringWithFormat:@"%d",pageNo]} completeHandler:^(id result, NSString *errorMessage) {
        
        if (result) {
            if([result[kErrorCode]intValue] == 0){
                NSArray *info = result[kinfo];
                if(info.count > 0){
                    foodListPageNo_= pageNo;
                    if(pageNo == 1){
                        [foodListArr_ removeAllObjects];
                    }
                    [foodListArr_ addObjectsFromArray:info];
                    self.queryFoodListCompleteHandler(foodListArr_,errorMessage);
                }else{
                    if (pageNo == 1) {
                        self.queryFoodListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.queryFoodListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.queryFoodListCompleteHandler(nil,result[kErrorMessage]);
            }
        }else{
            self.queryFoodListCompleteHandler(nil,errorMessage);
        }
    }];
    [self.queryFoodListQueue addOperation:operation];
}

// 查询指定页码的活动列表
- (void)queryActionListWithPageNo:(int)pageNo{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_ACTION_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kPageno:[NSString stringWithFormat:@"%d",pageNo]} completeHandler:^(id result, NSString *errorMessage) {
        
        if(result){
            if([result[kErrorCode]intValue] == 0){
                NSArray *info = result[kinfo];
                if (info.count > 0) {
                    actionListPageNo_ = pageNo;
                    if (pageNo == 1) {
                        [actionListArr_ removeAllObjects];
                    }
                    [actionListArr_ addObjectsFromArray:info];
                    self.queryActionListCompleteHandler(actionListArr_,errorMessage);
                }else{
                    if (pageNo == 1) {
                        self.queryActionListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.queryActionListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.queryActionListCompleteHandler(nil,errorMessage);
            }
        }else{
            self.queryActionListCompleteHandler(nil,errorMessage);
        }
        
    }];
    [self.queryActionListQueue addOperation:operation];
}

#pragma mark - Interface method

// 刷新美食列表
- (void)refreshFoodList{
    if (self.queryFoodListQueue.operationCount > 0) {
        self.queryFoodListCompleteHandler(nil,nil);
        return;
    }
    [self queryFoodListWithPageNo:1];
}

// 查询下一页美食列表
- (void)queryNextPageFoodList{
    if (self.queryFoodListQueue.operationCount > 0) {
        self.queryFoodListCompleteHandler(nil,nil);
        return;
    }
    [self queryFoodListWithPageNo:foodListPageNo_ + 1];
}

// 取消查询美食列表
- (void)cancelQueryFoodList{
    [self.queryFoodListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消查询美食详情
- (void)cancelQueryFoodDetail{
    [self.queryFoodDetailQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 根据编号查询美食详情
- (void)queryFoodDetailById:(NSString *)foodId
            completeHandler:(DataHandleComplete)completeHandler{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_FOOD_DETAIL requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kFoodId:foodId} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kinfo],errorMessage);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [self.queryFoodDetailQueue addOperation:operation];
}

// 发布美食详情
- (void)sendFoodNotiSubj:(NSString *)notiSubj notiCont:(NSString *)notiCont fileInfoArr:(NSArray *)fileInfoArr completeHandler:(DataHandleComplete)completeHandler uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler {
    
    NSLog(@"%@",NET_IF_KG_FOOD_SEND);
    
    uploadProcessingHandler_ = uploadProcessHandler;
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_FOOD_SEND requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    
    
    if (fileInfoArr) {
        // 1. 执行文件上传操作
        uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoArr completeHandler:^(id result, NSString *errorMessage) {
            
            [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
            
            if (errorMessage) {
                [self.sendInfoQueue cancelAllOperations];
                completeHandler(nil,errorMessage);
            }else if(result){
                NSError *err;
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
                if (err) {
                    completeHandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
                }else{
                    NSLog(@"文件上传结果：%@",dict);
                    // 检查errorCode
                    if ([dict[kErrorCode]intValue] == 0) {
                        NSMutableString *fileIdsStr = [NSMutableString string];
                        NSArray *infoArr = dict[kinfo];
                        for (NSDictionary *fileDict in infoArr) {
                            [fileIdsStr appendString:fileDict[kFileId]];
                            [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                        }
                        NSLog(@"fileIds:%@",fileIdsStr);
                        if (!notiSubj && !notiCont) {
                             operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFileIds:fileIdsStr};
                        }else{
                            if (notiCont && !notiSubj) {
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFoodCont:notiCont,kFileIds:fileIdsStr};
                            }else if(!notiCont && notiSubj){
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFoodSubj:notiSubj,kFileIds:fileIdsStr};
                            }else{
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFoodSubj:notiSubj,kFoodCont:notiCont,kFileIds:fileIdsStr};
                            }
                        }
                        
                        NSLog(@"%@",operation -> paramsDict_);
                       
                    }else{
                        completeHandler(nil,dict[kErrorMessage]);
                    }
                }
            }
        }];
        
        [operation addDependency:uploadOperation_];
        
        // 添加观察者
        [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
        
        [self.sendInfoQueue addOperation:uploadOperation_];
        
    }else{ // 图片不存在则必须提交美食内容
        operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kFoodSubj:notiSubj,kFoodCont:notiCont};
    }
    
    [self.sendInfoQueue addOperation:operation];
}

// 根据编号删除美食详情
- (void)removeFoodById:(NSString *)foodId
       completeHandler:(DataHandleComplete)completeHandler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_FOOD_DELETE requestMethod:NET_REQUEST_POST paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kFoodId:foodId} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [[[NSOperationQueue alloc]init]addOperation:operation];
}


// 刷新活动列表
- (void)refreshActionList{
    if (self.queryActionListQueue.operationCount > 0) {
        self.queryActionListCompleteHandler(nil,nil);
    }
    [self queryActionListWithPageNo:1];
}

// 查询下一页活动列表
- (void)queryNextPageActionList{
    if (self.queryActionListQueue.operationCount > 0) {
        self.queryActionListCompleteHandler(nil,nil);
    }
    [self queryActionListWithPageNo:actionListPageNo_ + 1];
}

// 取消查询活动列表
- (void)cancelQueryActionList{
    [self.queryActionListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消查询活动详情
- (void)cancelQueryActionDetail{
    [self.queryActionDetailQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 根据编号查询活动详情
- (void)queryActionDetailById:(NSString *)actionId
              completeHandler:(DataHandleComplete)completeHandler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_ACTION_DETAIL requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kActiId:actionId} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kinfo],errorMessage);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [self.queryActionDetailQueue addOperation:operation];
}

// 发布活动信息
- (void)sendActionNotiSubj:(NSString *)notiSubj
                  notiCont:(NSString *)notiCont
               fileInfoArr:(NSArray *)fileInfoArr
           completeHandler:(DataHandleComplete)completeHandler
   uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler{
    
    uploadProcessingHandler_ = uploadProcessHandler;
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_ACTION_SEND requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    
    if (fileInfoArr) {
        // 1. 执行文件上传操作
        uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoArr completeHandler:^(id result, NSString *errorMessage) {
            
            [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
            
            if (errorMessage) {
                [self.sendInfoQueue cancelAllOperations];
                completeHandler(nil,errorMessage);
            }else if(result){
                NSError *err;
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
                if (err) {
                    completeHandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
                }else{
                    NSLog(@"文件上传结果：%@",dict);
                    // 检查errorCode
                    if ([dict[kErrorCode]intValue] == 0) {
                        NSMutableString *fileIdsStr = [NSMutableString string];
                        NSArray *infoArr = dict[kinfo];
                        for (NSDictionary *fileDict in infoArr) {
                            [fileIdsStr appendString:fileDict[kFileId]];
                            [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                        }
                        NSLog(@"fileIds:%@",fileIdsStr);
                        if (!notiSubj && !notiCont) {
                            operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFileIds:fileIdsStr};
                        }else{
                            if (notiCont && !notiSubj) {
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kActiCont:notiCont,kFileIds:fileIdsStr};
                            }else if(!notiCont && notiSubj){
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kActiSubj:notiSubj,kFileIds:fileIdsStr};
                            }else{
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kActiSubj:notiSubj,kActiCont:notiCont,kFileIds:fileIdsStr};
                            }
                        }
                    }else{
                        completeHandler(nil,dict[kErrorMessage]);
                    }
                }
            }
        }];
        
        [operation addDependency:uploadOperation_];
        
        // 添加观察者
        [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
        
        [self.sendInfoQueue addOperation:uploadOperation_];
        
    }else{ // 图片不存在则必须提交活动内容
        operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kActiSubj:notiSubj,kActiCont:notiCont};
    }
    
    [self.sendInfoQueue addOperation:operation];
}

// 根据编号删除活动
- (void)removeActionById:(NSString *)actionId
         completeHandler:(DataHandleComplete)completeHandler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_ACTION_DELETE requestMethod:NET_REQUEST_POST paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kActiId:actionId} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [[[NSOperationQueue alloc]init]addOperation:operation];
}

// 查询幼儿园简介
- (void)queryKgDetail:(DataHandleComplete)completeHandler{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_BRIE_INFO requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kinfo],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [[[NSOperationQueue alloc]init]addOperation:operation];
}

// 发布幼儿园简介
- (void)sendKgInfoWithBrieCont:(NSString *)brieCont fileInfoArr:(NSArray *)fileInfoArr completeHandler:(DataHandleComplete)completeHandler uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler{
    
    uploadProcessingHandler_ = uploadProcessHandler;
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_BRIE_SEND requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    
    if (fileInfoArr) {
        // 1. 执行文件上传操作
        uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoArr completeHandler:^(id result, NSString *errorMessage) {
            
            [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
            
            if (errorMessage) {
                [self.sendInfoQueue cancelAllOperations];
                completeHandler(nil,errorMessage);
            }else if(result){
                NSError *err;
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
                if (err) {
                    completeHandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
                }else{
                    NSLog(@"文件上传结果：%@",dict);
                    // 检查errorCode
                    if ([dict[kErrorCode]intValue] == 0) {
                        NSMutableString *fileIdsStr = [NSMutableString string];
                        NSArray *infoArr = dict[kinfo];
                        for (NSDictionary *fileDict in infoArr) {
                            [fileIdsStr appendString:fileDict[kFileId]];
                            [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                        }
                        NSLog(@"fileIds:%@",fileIdsStr);
                        if (!brieCont) {
                            operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFileIds:fileIdsStr};
                        }else{
                            operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kBrieCont:brieCont,kFileIds:fileIdsStr};
                        }
                    }else{
                        completeHandler(nil,dict[kErrorMessage]);
                    }
                }
            }
        }];
        
        [operation addDependency:uploadOperation_];
        
        // 添加观察者
        [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
        
        [self.sendInfoQueue addOperation:uploadOperation_];
        
    }else{ // 图片不存在则必须提交活动内容
        operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kBrieCont:brieCont};
    }
    
    [self.sendInfoQueue addOperation:operation];
    
}

// 更新园所信息
- (void)updateKgInfoWithBrieId:(NSString*)brieId
                      BrieCont:(NSString*)brieCont
                   fileInfoArr:(NSArray*)fileInfoArr
               completeHandler:(DataHandleComplete)completeHandler
       uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler;{
    
    
    uploadProcessingHandler_ = uploadProcessHandler;
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_BRIE_UPDATE requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    
    if (fileInfoArr) {
        // 1. 执行文件上传操作
        uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoArr completeHandler:^(id result, NSString *errorMessage) {
            
            [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
            
            if (errorMessage) {
                [self.sendInfoQueue cancelAllOperations];
                completeHandler(nil,errorMessage);
            }else if(result){
                NSError *err;
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
                if (err) {
                    completeHandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
                }else{
                    NSLog(@"文件上传结果：%@",dict);
                    // 检查errorCode
                    if ([dict[kErrorCode]intValue] == 0) {
                        NSMutableString *fileIdsStr = [NSMutableString string];
                        NSArray *infoArr = dict[kinfo];
                        for (NSDictionary *fileDict in infoArr) {
                            [fileIdsStr appendString:fileDict[kFileId]];
                            [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                        }
                        NSLog(@"fileIds:%@",fileIdsStr);
                        if (!brieCont) {
                            operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kBrieId:brieId,kFileIds:fileIdsStr};
                        }else{
                            operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kBrieCont:brieCont,kBrieId:brieId,kFileIds:fileIdsStr};
                        }
                    }else{
                        completeHandler(nil,dict[kErrorMessage]);
                    }
                }
            }
        }];
        
        [operation addDependency:uploadOperation_];
        
        // 添加观察者
        [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
        
        [self.sendInfoQueue addOperation:uploadOperation_];
        
    }else{ // 图片不存在则必须提交活动内容
        operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kBrieId:brieId,kBrieCont:brieCont};
    }
    
    [self.sendInfoQueue addOperation:operation];
    
}

// 刷新院长信箱列表
- (void)refreshSuggestList{
    if (self.querySuggestListQueue.operationCount > 0) {
        self.querySuggestListCompleteHandler(nil,nil);
    }
    [self querySuggestListWithPageNo:1];
}

// 获取下一页园长信箱列表
- (void)queryNextPageSuggestList{
    if (self.querySuggestListQueue.operationCount > 0) {
        self.querySuggestListCompleteHandler(nil,nil);
    }
    [self querySuggestListWithPageNo:suggestListPageNo_ + 1];
}

// 取消查询园长信箱列表
- (void)cancelQuerySuggestList{
    [self.querySuggestListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 根据编号查询园长信箱详情
- (void)querySuggestInfoById:(NSString *)suggId completeHandler:(DataHandleComplete)completeHandler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_SUGGEST_DETAIL requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kSuggId:suggId} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kinfo],errorMessage);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [self.querySuggestInfoQueue addOperation:operation];
}

// 取消查询园长信箱详情
- (void)cancelQuerySuggestInfo{
    [self.querySuggestInfoQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 发布园长信箱
- (void)sendSuggestInfoWithSuggSubj:(NSString *)suggSubj
                           SuggCont:(NSString *)suggCont
                        fileInfoArr:(NSArray *)fileInfoArr
                    completeHandler:(DataHandleComplete)completeHandler
            uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler{
    
    uploadProcessingHandler_ = uploadProcessHandler;
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_SUGGEST_SEND requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    
    if (fileInfoArr) {
        // 1. 执行文件上传操作
        uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoArr completeHandler:^(id result, NSString *errorMessage) {
            
            [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
            
            if (errorMessage) {
                [self.sendInfoQueue cancelAllOperations];
                completeHandler(nil,errorMessage);
            }else if(result){
                NSError *err;
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
                if (err) {
                    completeHandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
                }else{
                    NSLog(@"文件上传结果：%@",dict);
                    // 检查errorCode
                    if ([dict[kErrorCode]intValue] == 0) {
                        NSMutableString *fileIdsStr = [NSMutableString string];
                        NSArray *infoArr = dict[kinfo];
                        for (NSDictionary *fileDict in infoArr) {
                            [fileIdsStr appendString:fileDict[kFileId]];
                            [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                        }
                        NSLog(@"fileIds:%@",fileIdsStr);
                        if (!suggSubj && !suggCont) {
                            operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kFileIds:fileIdsStr};
                        }else{
                            if (suggCont && !suggSubj) {
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kSuggCont:suggCont,kFileIds:fileIdsStr};
                            }else if(!suggCont && suggSubj){
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kSuggSubj:suggSubj,kFileIds:fileIdsStr};
                            }else{
                                operation -> paramsDict_ = @{kAccount:userInfo.account,kPassword:userInfo.password,kSuggSubj:suggSubj,kSuggCont:suggCont,kFileIds:fileIdsStr};
                            }
                        }
                    }else{
                        completeHandler(nil,dict[kErrorMessage]);
                    }
                }
            }
        }];
        
        [operation addDependency:uploadOperation_];
        
        // 添加观察者
        [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
        
        [self.sendInfoQueue addOperation:uploadOperation_];
        
    }else{ // 图片不存在则必须提交活动内容
        operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kSuggSubj:suggSubj,kSuggCont:suggCont};
    }
    
    [self.sendInfoQueue addOperation:operation];
}

// 根据编号删除指定园长信箱
- (void)removeSuggestById:(NSString *)suggestId
          completeHandler:(DataHandleComplete)completeHandler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_KG_SUGGEST_REMOVE requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kSuggId:suggestId} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kErrorMessage],nil);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,errorMessage);
        }
        
    }];
    [[[NSOperationQueue alloc]init]addOperation:operation];
}


#pragma mark - Property method

- (NSOperationQueue*)queryFoodListQueue{
    if (!_queryFoodListQueue) {
        _queryFoodListQueue = [[NSOperationQueue alloc]init];
    }
    return _queryFoodListQueue;
}

- (NSOperationQueue*)queryActionListQueue{
    if (!_queryActionListQueue) {
        _queryActionListQueue = [[NSOperationQueue alloc]init];
    }
    return _queryActionListQueue;
}

- (NSOperationQueue*)queryFoodDetailQueue{
    if (!_queryFoodDetailQueue) {
        _queryFoodDetailQueue = [[NSOperationQueue alloc]init];
    }
    return _queryFoodDetailQueue;
}

- (NSOperationQueue*)queryActionDetailQueue{
    if (!_queryActionDetailQueue) {
        _queryActionDetailQueue = [[NSOperationQueue alloc]init];
    }
    return _queryActionDetailQueue;
}

- (NSOperationQueue*)sendInfoQueue{
    if (!_sendInfoQueue) {
        _sendInfoQueue = [[NSOperationQueue alloc]init];
    }
    return _sendInfoQueue;
}

- (NSOperationQueue*)querySuggestListQueue{
    if (!_querySuggestListQueue) {
        _querySuggestListQueue = [[NSOperationQueue alloc]init];
    }
    return _querySuggestListQueue;
}

- (NSOperationQueue*)querySuggestInfoQueue{
    if(!_querySuggestInfoQueue){
        _querySuggestInfoQueue = [[NSOperationQueue alloc]init];
    }
    return _querySuggestInfoQueue;
}

#pragma mark - Life Cycle method

- (id)init{
    if (self = [super init]) {
        foodListArr_ = [NSMutableArray array];
        actionListArr_ = [NSMutableArray array];
        suggestListArr_ = [NSMutableArray array];
    }
    return self;
}

// 观察者回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"progress"]) {
        IMB_NetWorkAccessOperationWithFilesUpload *operation = object;
        uploadProcessingHandler_(operation.progress);
    }
}

- (void)dealloc
{
    NSLog(@"BN_KinderGartenHelper dealloc ...");
}
@end
